package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TcwDepositPlatformAccount;
import com.ys.entity.sys.TbStaticConfig;
import com.ys.enume.wanli.WanliAuthorizedStatusEnum;
import com.ys.enume.wanli.WfSubUserEnum;
import com.ys.mapper.finance.TcwDepositPlatformAccountMapper;
import com.ys.service.finance.TcwDepositPlatformAccountService;
import com.ys.util.wanli.WanliUtils;
import com.ys.vo.ApiResponse;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("tcwDepositAccountPlatformService")
public class TcwDepositPlatformAccountServiceImpl implements TcwDepositPlatformAccountService {

    @Autowired
    private TcwDepositPlatformAccountMapper tcwDepositPlatformAccountMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {

        TcwDepositPlatformAccount tcwDepositPlatformAccount = JSON.parseObject(JSON.toJSONString(params), TcwDepositPlatformAccount.class);
//        String loginUserId = ShiroUtils.getUserId();
//        TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
//        tcwDepositAccountPlatform.setCreateChangeld(tsUserChange.getId());
        tcwDepositPlatformAccountMapper.insert(tcwDepositPlatformAccount);
        return R.ok(AddDto.closeAndReload());
    }

    /**
     * 修改
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {


        TcwDepositPlatformAccount tcwDepositPlatformAccount = JSON.parseObject(JSON.toJSONString(params), TcwDepositPlatformAccount.class);

        tcwDepositPlatformAccountMapper.update(tcwDepositPlatformAccount);
        return R.ok(AddDto.closeAndReload());
    }

    /**
     * 集成商可调用此接口获取店铺信息以及店铺关联账号的信息。
     */
    @Override
    public ApiResponse inquiryStore(TcwDepositPlatformAccount tcwDepositPlatformAccount, Date date) {
        ApiResponse apiResponse = new ApiResponse();
        JSONArray storeInformationList;
        JSONArray accountInformationList;
        String authorizedStatus;
        String accountNo;
        JSONObject storeInformation;
        JSONObject storeInformationListZj;
        try {
            apiResponse.setCode(0);
            apiResponse.setMessage("获取成功!");
            String content = "";
            int pageNumber = 1;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pageSize", 100);

            //查询万里汇莫程账号clientId
            TbStaticConfig tbStaticConfigClientId = new TbStaticConfig()
                    .setKeyOne("wanli-mid")
                    .setKeyTwo("wanli-mid-clientId")
                    .queryFirst();

            String httpMethod = WfSubUserEnum.Type.POST.value;
            //主账号有子账号
            String clientId = tbStaticConfigClientId.getName();
            String uriWithQueryString = WfSubUserEnum.Type.INQUIRYSTORE.value;
            String connectedCustomerId = tcwDepositPlatformAccount.getMid();

            boolean falx = true;
            while (falx) {
                jsonObject.put("pageNumber", pageNumber);
                content = JSONObject.toJSONString(jsonObject);
                String result = WanliUtils.result(content, httpMethod, uriWithQueryString, clientId, connectedCustomerId);
                JSONObject resultJO = JSON.parseObject(result);
                JSONObject resultState = JSON.parseObject(resultJO.get("result").toString());
                //判断是否成功
                if (!resultState.get("resultStatus").toString().equals(WfSubUserEnum.Type.S.value)) {
                    apiResponse.setCode(500);
                    apiResponse.setMessage(resultState.get("resultMessage").toString());
                    return apiResponse;
                }



                //获取账号信息
                storeInformationList = JSON.parseArray(resultJO.get("storeInformation").toString());
                for (int i = 0; i < storeInformationList.size(); i++) {
                    storeInformationListZj = JSON.parseObject(storeInformationList.get(i).toString());
                    //获取状态
                    authorizedStatus = storeInformationListZj.get("authorizedStatus").toString();
                    accountInformationList = JSON.parseArray(storeInformationListZj.get("accountInformation").toString());
                    for (int j = 0; j < accountInformationList.size(); j++) {
                        //获取账户信息
                        storeInformation = JSON.parseObject(accountInformationList.get(j).toString());
                        //存款账号
                        accountNo = storeInformation.get("accountNo").toString();
                        if (tcwDepositPlatformAccount.getVyhzh().equals(accountNo)){
                            if (WanliAuthorizedStatusEnum.AuthorizedStatus.NEVER_AUTHORIZED.value.equals(authorizedStatus)) {
                                tcwDepositPlatformAccount.setAuthorizedStatus(0);
                            }else {
                                tcwDepositPlatformAccount.setAuthorizedStatus(1);
                            }
                            tcwDepositPlatformAccountMapper.update(tcwDepositPlatformAccount);
                        }
                    }
                }
                pageNumber++;

                //判断是否还需要再获取数据
                if (Integer.valueOf(resultJO.get("totalPageNumber").toString()) < pageNumber) {
                    falx = false;
                    continue;
                }
            }
        } catch (Exception e) {
            apiResponse.setCode(500);
            apiResponse.setMessage(e.getMessage());
            return apiResponse;
        }
        return apiResponse;
    }


}
